<template>
  <div>
    <TableBar>
      <el-button @click="visible = true" type="primary" icon="el-icon-plus">添加</el-button>
      <el-button @click="onShowUpload" icon="el-icon-upload">批量上传</el-button>
      <el-button @click="onMulRemove" icon="el-icon-delete">移除</el-button>
    </TableBar>
    <el-table :data="tableData" @selection-change="handleSelectionChange" style="width: 100%">
      <el-table-column fixed="left" type="selection" width="55"></el-table-column>
      <el-table-column fixed="left" type="index" width="55">序号</el-table-column>

      <el-table-column prop="userid" label="用户ID"></el-table-column>

      <el-table-column prop="teacherid" label="教师工号"></el-table-column>

      <el-table-column prop="username" label="教师姓名"></el-table-column>

      <el-table-column prop="college_name" label="学院名称"></el-table-column>

      <el-table-column prop="school_name" label="学校名称"></el-table-column>

      <!-- <el-table-column prop="choose_time" label="添加时间"></el-table-column> -->
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change=handleSizeChange
      :current-page.sync="search.page"
      :page-size="search.page_size"
      :total="total"
    ></el-pagination>
    <el-dialog @close="onClose" width="90%" title="添加授课教师" :visible.sync="visible">
      <Searchbar>
        <el-autocomplete
          class="inline-input"
          v-model="selectedSchool"
          :fetch-suggestions="querySchool"
          placeholder="请输入学校名称"
          @select="selectSchool"
        ></el-autocomplete>

        <el-autocomplete
          class="inline-input"
          v-model="selectedCollege"
          :fetch-suggestions="queryCollege"
          placeholder="请输入学院名称"
          @select="selectCollege"
        ></el-autocomplete>
        <el-input @change="getList" v-model="search.keyword" placeholder="请按教师ID/教师工号/教师姓名查找"></el-input>
        <el-button class="search-btn" @click="getList" type="primary">查询</el-button>
      </Searchbar>
      <el-table :data="dTableData" @selection-change="dHandleSelectionChange" style="width: 100%">
        <el-table-column fixed="left" type="selection" width="55"></el-table-column>
        <el-table-column fixed="left" type="index" width="55">序号</el-table-column>
        <el-table-column prop="userid" label="教师ID" ></el-table-column>
        <el-table-column prop="teacherid" label="教师工号" ></el-table-column>
        <el-table-column prop="college_name" label="学院名称" ></el-table-column>
        <el-table-column prop="school_name" label="学校名称" ></el-table-column>
      </el-table>
      <el-button style="margin-left: 10px;" size="small" type="primary" @click="submitDialog">确定</el-button>
      <el-button style="margin-left: 10px;" size="small"  @click="onClose">取消</el-button>
    </el-dialog>
    <MulUpload @onClose="onHideUpload" type='3' temp='5' action="/api/object/batch/create/" :show="showUpload" />
  </div>
</template>
<script>
import MulUpload from "@/components/MulUpload";
import showEditMixin from "@/mixins/showEditMixin";
import uploadMixin from "@/mixins/uploadMixin";
import pageMixin from "@/mixins/pageMixin";
import { teacherModel, courseModel } from "@/api/models";
import CreateFilter from "@/mixins/CreateFilter";
import TableBar from "@/components/TableBar";
import selectionChange from '@/mixins/selectionChang.js';
import Searchbar from "@/components/Searchbar";
import {Message} from 'element-ui';

export default {
  mixins: [showEditMixin, uploadMixin, pageMixin, CreateFilter, selectionChange],
  components: { MulUpload, TableBar, Searchbar},
  data() {
    return {
      visible: false,
      tableData: [],
      total: 0,
      search: {
        selectedschool_id: null,
        selectedcollege_id: null,
        selectedmajor_id: null,
        course_id: null,
        keyword: null
      },
      dMultipleSelection: [],
      dTableData: []
    };
  },
  props: {
    course_id: {
      type: [Number, String],
      default: null
    }
  },
  watch: {
    course_id () {
      this.courseStus();
    }
  },
  methods: {
    getList() {
      console.log("getList");
      teacherModel.list(
        this.search,
        res => {
          this.dTableData = res.data.map(v => {
            return v;
          });
        },
        err => {
          console.log(err);
        }
      );
    },
    courseStus(){
     courseModel.teachers(
        this.course_id,
        res => {
          this.total = res.total_records;
          this.tableData = res.data.map(v => {
            return v;
          });
        },
        err => {
          console.log(err);
        }
      );
    }, 
    dHandleSelectionChange(val){
      this.dMultipleSelection = val
    },
    onClose() {
      this.visible = false
    },
    teacherAdd(users, action) {
      courseModel.teacherAdd({
        course_id: this.course_id,
        users: users,
        action: action
      }, res => {
        Message.warning(res.message);
        this.onClose();
        this.courseStus()
      })
    },
    submitDialog() {
      if(this.dMultipleSelection.length == 0) {
        return Message.warning("先选择添加教师");
      }
      this.teacherAdd(this.dMultipleSelection.map(v=>{return v.userid}), 1);
    },
    onMulRemove() {
      if(this.multipleSelection.length == 0) {
        return Message.warning("先选择添加教师");
      }
      this.teacherAdd(this.multipleSelection.map(v=>{return v.userid}), 2);
    }
  }
};
</script>
<style lang="scss" scoped>
.el-dialog__body .el-input{
  width: auto !important;
}
</style>